package com.dushiguang.findhoney.Utils.CacheData;

import android.content.Context;
import android.database.Cursor;

import com.dushiguang.findhoney.Utils.DataStorage.Db.DbUtil;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.db.annotation.Table;
import com.lidroid.xutils.db.sqlite.SqlInfo;
import com.lidroid.xutils.db.sqlite.WhereBuilder;
import com.lidroid.xutils.exception.DbException;

/**
 * Created by ex-dushiguang201 on 2015-12-09.
 */
public class CacheDataOp {

    private CacheDataOp() {

    }

    /**
     * 数据库中缓存的数据
     * @param dataType 要缓存的数据类型,比如用户信息,或者登录信息等
     * @param cacheData 要缓存的数据
     * @param dataTag 数据标识
     * @return
     */
    public static boolean cacheData(Context context, int dataType, String cacheData, String dataTag) {
        Cursor cursor = null;
        DbUtils dbUtils;

        DbUtil initDb = new DbUtil(context);
        dbUtils = initDb.getDb();

        if (dbUtils == null) {
            return false;
        }

        try {
            dbUtils.createTableIfNotExist(DataCacheBean.class);

            DataCacheBean bean = new DataCacheBean();

            bean.type = dataType;
            bean.dataFlag = 1;
            bean.cacheData = cacheData;
            bean.cacheTag = dataTag;
            bean.timeStamp = String.valueOf(System.currentTimeMillis() / 1000);

            // 检测数据库是否有该类型数据
            cursor = dbUtils.execQuery(new SqlInfo(
                    "select count(id) from cache_data where type = ? and cacheTag = ? and dataFlag = 1", bean.type,
                    bean.cacheTag));

            int count = 0;
            if (null != cursor && cursor.moveToNext()) {
                count = cursor.getInt(0);
            }

            if (0 == count) {
                dbUtils.save(bean);
            } else {
                dbUtils.update(bean, WhereBuilder.b("dataType", "=", bean.type)
                        .and("cacheTag", "=", bean.cacheTag)
                        .and("dataFlag", "=", bean.dataFlag),"cacheData", "timeStamp");
                return true;
            }
        } catch (DbException e) {
            e.printStackTrace();
        } finally {
            if (null != cursor) {
                cursor.close();
                cursor = null;
            }
        }
        return false;
    }

    public static String getCacheDataString(Context context, int type, String dataTag) {
        Cursor cursor = null;
        DbUtils dbUtils;

        DbUtil initDb = new DbUtil(context);
        dbUtils = initDb.getDb();

        String cacheData = "";
        try {
            dbUtils.createTableIfNotExist(DataCacheBean.class);
            cursor = dbUtils.execQuery(new SqlInfo(
                    "select cacheData from t_cache_data where type = ? and cacheTag = ? and dataFlag = 1 limit 1",
                    type, dataTag));
            if (null != cursor && cursor.moveToNext()) {
                cacheData = cursor.getString(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != cursor) {
                cursor.close();
                cursor = null;
            }
        }
        return cacheData;
    }

    /**
     * 缓存数据元
     */
    @Table(name = "cache_data")
    public static class DataCacheBean {
        int    id;       // 数据id
        int    type;     // 数据类型
        int    dataFlag; // 1为最新数据，0为老旧数据
        String cacheData; // 缓存数据
        String cacheTag; // 相同类型时的数据标识
        String timeStamp; // 更新时间戳

        /** 缓存数据是否过期，过期时间6小时 */
        public boolean isCacheDataOutOfDate() {
            try {
                long cacheTime = Long.parseLong(timeStamp);
                long currTime = System.currentTimeMillis() / 1000;
                return currTime - cacheTime >= 6 * 60 * 60;
            } catch (Exception e) {
            }
            return true;
        }
    }
}
